package com.leyou.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class LeyouCorsFilter {
    @Bean
    public CorsFilter corsFilter(){
        //初始化cors配置对象
        CorsConfiguration config = new CorsConfiguration();
        //允许跨域的域名，如果要携带cookie，不能写*。*： 代表所有域名可以跨域
        config.addAllowedOrigin("http://manage.leyou.com");
        config.addAllowedOrigin("http://www.leyou.com");
        //允许所有头信息
        config.addAllowedHeader("*");
        //允许携带cookie信息
        config.setAllowCredentials(true);
        //表示所有方法都允许，get、post、put……
        config.addAllowedMethod("*");
        //配置源
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**",config);
        //过滤配置
        return new CorsFilter( configSource);
    }
}
